package com.daka.controller;

import com.daka.entity.Order;
import com.daka.entity.Project;
import com.daka.entity.Team;
import com.daka.entity.User;
import com.daka.enumerate.StatusCode;
import com.daka.service.OrderService;
import com.daka.service.ProjectService;
import com.daka.service.TeamService;
import com.daka.service.UserService;
import com.daka.vo.DataResponseVo;
import com.daka.vo.ListDataResponseVo;
import com.daka.vo.response.DonationSimpVo;
import com.daka.vo.response.TeamDetail;
import com.daka.vo.response.TeamSimpVo;
import com.daka.vo.response.TeamUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by yangx
 * CreateTime: 2016/12/12 17:08
 */

@Controller
@RequestMapping("/team")
@Slf4j
public class TeamController {

    @Resource
    private TeamService teamService;

    @Resource
    private ProjectService projectService;

    @Resource
    private OrderService orderService;

    @Resource
    private UserService userService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/findList", consumes = "application/json")
    public ListDataResponseVo<TeamSimpVo> findList() {
        List<TeamSimpVo> resultList = new ArrayList<>();

        List<Team> list = teamService.findList();

        TeamSimpVo temp;
        int a = 1;
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                temp = TeamSimpVo.build(list.get(i), a);
            } else {
                if (Objects.equals(list.get(i - 1).getTotalMoney(), list.get(i).getTotalMoney())) {
                    temp = TeamSimpVo.build(list.get(i), a);
                } else {
                    a = a + 1;
                    temp = TeamSimpVo.build(list.get(i), a);
                }
            }
            List<Order> list1 = orderService.selectByTeamId(list.get(i).getId());
            temp.setAmount(list1.size());
            resultList.add(temp);
        }

        return new ListDataResponseVo<>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), resultList);
    }

    /**
     * 队伍详情页
     *
     * @param team
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/findById", consumes = "application/json")
    public DataResponseVo<TeamDetail> findById(@RequestBody Team team) {
        TeamDetail teamDetail = null;
        if (team != null && team.getId() != null) {
            team = teamService.findById(team.getId());
            Project project = projectService.findByTeamId(team.getId());

            if (project != null) {

                Order order = new Order();
                order.setProjectId(project.getId());
                Integer number = orderService.count(order);

                User leaderUser = userService.selectById(team.getLeader());
                teamDetail = TeamDetail.build(team, project, number, leaderUser.getName());
                //捐款人次
                List<Order> list1 = orderService.selectByTeamId(team.getId());
                teamDetail.setAmount(list1.size());
            }

        }
        return new DataResponseVo<>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), teamDetail);
    }

    /**
     * 队伍捐赠记录页面
     *
     * @param team 只用teamId
     * @return 页面所需全部数据
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/donationList", consumes = "application/json")
    public DataResponseVo<TeamUserVo> donationList(@RequestBody Team team) {

        Order orderParam = new Order();
        orderParam.setTeamId(team.getId());
        List<Order> orderList = orderService.selectOrderList(orderParam);

        List<DonationSimpVo> resultList = new ArrayList<>();
        BigDecimal total = BigDecimal.ZERO;
        for (Order order : orderList) {
            total = total.add(order.getMoney());

            User user = userService.selectById(order.getUserId());

            DonationSimpVo donationSimpVo = DonationSimpVo.build(user, order.getMoney());
            resultList.add(donationSimpVo);
        }

        Integer unCount = userService.selectUnDonationCount(team.getId());

        team = teamService.findById(team.getId());
        TeamUserVo teamUserVo = TeamUserVo.build(resultList, total, orderList.size(), unCount, team.getName());

        return new DataResponseVo<>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), teamUserVo);
    }
}
